package com.first.font.filter;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @program: finrst-education-parent
 * @description:
 * @author: 张玉森
 * @create: 2023-05-23 09:47
 **/
@Component
public class LoginFilter implements GlobalFilter, Ordered {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        //获取请求路径
        String path = request.getPath().toString();
        //该路径是否为放行路径
        if(path!=null && path.equals("/sso/admin/qianLogin" )){//因为不满足断言
            return chain.filter(exchange);// 放行
        }
        if(path!=null && path.startsWith("/student")){//因为不满足断言
            return chain.filter(exchange);// 放行
        }
        if(path!=null && path.startsWith("/home/qian")){//因为不满足断言
            return chain.filter(exchange);// 放行
        }
        if(path!=null && path.startsWith("/order/play-history/chapter/findChapterById/")){//因为不满足断言
            return chain.filter(exchange);// 放行
        }
        if(path!=null && path.startsWith("/order/course/queryAllCourseAndTeacherName")){//因为不满足断言
            return chain.filter(exchange);// 放行
        }
        if(path!=null && path.startsWith("/order/video")){//因为不满足断言
            return chain.filter(exchange);// 放行
        }
        if(path!=null && path.startsWith("/order/chapter/findChapterById")){//因为不满足断言
            return chain.filter(exchange);// 放行
        }
        if(path!=null && path.startsWith("/order/video/queryAllVideoByCourseId")){//因为不满足断言
            return chain.filter(exchange);// 放行
        }
        if(path!=null && path.startsWith("/core/course/fontFindCourse")){//因为不满足断言
            return chain.filter(exchange);// 放行
        }
        if(path!=null && path.startsWith("/core/course/getAllMenu")){//因为不满足断言
            return chain.filter(exchange);// 放行
        }
        if(path!=null && path.startsWith("/core/course/findTeacherByTeacherId")){//因为不满足断言
            return chain.filter(exchange);// 放行
        }
        if(path!=null && path.startsWith("/core/course/findCourseByTeacherId")){//因为不满足断言
            return chain.filter(exchange);// 放行
        }



        //获取当前请求是否携带token令牌---请求头中放置
        String token = request.getHeaders().getFirst("homeToken");
        if(token!=null &&redisTemplate.hasKey("home::" + token)){ //注意值:admin随便写。
            //延迟过期时间
            redisTemplate.expire("home::"+token,300, TimeUnit.MINUTES);
            return chain.filter(exchange);// 放行
        }
        //请先登录----
        //3.2封装返回数据
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "未登录");
        map.put("code", 401);
        //3.3作JSON转换--fastjson
        byte[] bytes = JSON.toJSONString(map).getBytes(StandardCharsets.UTF_8);
        //3.4调用bufferFactory方法,生成DataBuffer对象
        DataBuffer buffer = response.bufferFactory().wrap(bytes);
        //4.调用Mono中的just方法,返回要写给前端的JSON数据
        return response.writeWith(Mono.just(buffer));
    }

    //获取优先级 值约大优先级越高
    @Override
    public int getOrder() {
        return 0;
    }
}
